package com.dsh.authorityManagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dsh.authorityManagement.dao.DoctorDao;
import com.dsh.authorityManagement.entity.AdminDoctor;
import com.dsh.authorityManagement.entity.vo.AdminOfficeVo;
import com.dsh.authorityManagement.entity.vo.OfficeVo;
import com.dsh.authorityManagement.entity.vo.Uid;
import com.dsh.authorityManagement.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DoctorImpl implements DoctorService {
    @Autowired
    DoctorDao doctorDao;

    @Override
    public void update(AdminDoctor adminDoctor) {
        AdminOfficeVo adminOfficeVo = doctorDao.selectById(adminDoctor.getUid());

        if (adminOfficeVo!=null) {
                String s = JSON.toJSONString(adminDoctor.getRoomids());
                adminDoctor.setRoomid(s);
                this.doctorDao.update(adminDoctor);
            }
            else {
                String s = JSON.toJSONString(adminDoctor.getRoomids());
                adminDoctor.setRoomid(s);
                this.doctorDao.insert(adminDoctor);

        }
//        JSONArray objects = JSON.parseArray(adminDoctor.getRoomid());
    }

    @Override
    public AdminOfficeVo selectById(int id) {
        AdminOfficeVo adminOfficeVo1 = doctorDao.selectById(id);
        List<Integer> integers = JSON.parseArray(adminOfficeVo1.getRoomid(), Integer.class);
        List<OfficeVo> list=new ArrayList<>();
        for (Integer integer : integers) {
            OfficeVo officeVo = doctorDao.selectByOfficeId(integer);
            if (officeVo!=null){
                list.add(officeVo);
            }
        }
        adminOfficeVo1.setOfficeVoList(list);
        return adminOfficeVo1;
    }

//    @Override
//    public int update(AdminDoctor adminDoctor) {
//        String s = JSON.toJSONString(adminDoctor.getRoomids());
//        adminDoctor.setRoomid(s);
//        int update = doctorDao.update(adminDoctor);
//        return update;
//    }
}
